// src/api/auth.ts
import {  post, get } from '@/utils/request'; // 修正拼写错误
import type { CustomRequestConfig } from '@/utils/request';

// 类型定义
export type UserType = 'student' | 'teacher' | 'admin';

export interface UserInfo {
    role: boolean;
    id: number;
    username: string;
    name: string;
    type: UserType;
    avatar: string;
    phone: string;
    email?: string;
    college?: string;
    major?: string;
}

export interface LoginCredentials {
    username: string;
    password: string;
}

export interface LoginResponse {
    success: boolean;
    message: string;
    token: string;
    user: {
        id: number;
        username: string;
        realName: string;
        role: UserType;
        phone: string;
        email?: string;
        avatar?: string;
    };
}

// 自定义请求配置
const authRequestConfig: CustomRequestConfig = {
    showLoading: true,
    ignoreError: false,
    retry: true,
    retryCount: 2,
    retryDelay: 1000,
};

// 真实登录函数
export const login = (credentials: LoginCredentials): Promise<LoginResponse> => {
    return post<LoginResponse>('/api/login', credentials, authRequestConfig);
};

// 注销函数
export const logout = (): Promise<void> => {
    return post<void>('/auth/logout', undefined, {
        ...authRequestConfig,
        showLoading: false
    });
};

// 获取用户信息函数
export const getUserInfo = (): Promise<UserInfo> => {
    return get<UserInfo>('/api/user-info', undefined, {
        ...authRequestConfig,
        retry: true
    });
};




